package com.gange.controller;


import com.gange.entity.Category;
import com.gange.service.ICategoryService;
import com.gange.utils.JSONUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 分类 前端控制器
 * </p>
 *
 * @author gange
 * @since 2022-07-23
 */
@RestController
@RequestMapping("/categories")
@Slf4j
public class CategoryController {
    /**
     * 服务对象
     */
    @Resource
    private ICategoryService categoryService;

    /**
     * 分类列表
     */
    @GetMapping
    public List<Category> categories() {
        log.info("进入查询类别列表方法..");
        List<Category> categories = categoryService.findAll();
        log.info("查询当前一级类别列表总数为: {}", categories.size());
        return categories;
    }

    /**
     * 根据id查询类别
     */
    @GetMapping("{id}")
    public Category category(@PathVariable("id") Integer id) {
        log.info("接收到的类别id: {}", id);
        Category category = categoryService.getById(id);
        log.info("查询到的类别信息: {}", JSONUtils.writeValueAsString(category));
        return category;
    }
}

